home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 8: LINUX Games / Linux Cubed Series 8 - LINUX Games.iso / games / x11 / networke / xfirepow.000 / xfirepow / xfirepower-0.84 / client / libsprite / init.c < prev    next >
C/C++ Source or Header  |  1995-12-17  |  12KB  |  385 lines

  1. #include "allincludes.h"
  2.  
  3. #ifdef FOURPLANEFIX
  4. static unsigned short extrared[8] = {0x00, 0x20, 0x40, 0x60, 0x80, 0xa0, 0xb0, 0xc0};
  5. static unsigned short extragreen[8] = {0x40, 0x60, 0x80, 0xa0, 0xb0, 0xc0, 0x00, 0x20};
  6. static unsigned short extrablue[8] = {0x80, 0xa0, 0xb0, 0xc0, 0x00, 0x20, 0x40, 0x60};
  7. #endif
  8.  
  9. static XFontStruct *
  10. find_font(oldf, fnts)
  11.     char   *oldf, **fnts;
  12. {
  13.     XFontStruct *fi;
  14.     char  **f;
  15.     fprintf(stderr, "xgalaga: Can't find font %s.  Trying others...\n",
  16.         oldf);
  17.     for (f = fnts; *f; f++) {
  18.     if (strcmp(*f, oldf) != 0) {
  19.         if ((fi = XLoadQueryFont(W_Display, *f)))
  20.         return fi;
  21.     }
  22.     }
  23.     printf("Error - can't find any font!\n");
  24.     return NULL;
  25. }
  26.  
  27. static void
  28. GetFonts()
  29. {
  30.     Font    regular, italic, bold, big;
  31.     int     i;
  32.     XGCValues values;
  33.     XFontStruct *fontinfo;
  34.     char   *fontname;
  35.  
  36.     fontname = NORMAL_FONT;
  37.     fontinfo = XLoadQueryFont(W_Display, fontname);
  38.     if (fontinfo == NULL) {
  39.     fontinfo = find_font(fontname, _nfonts);
  40.     }
  41.     if (fontinfo == NULL) {
  42.     printf("xgalaga: Can't find any fonts!\n");
  43.     exit(1);
  44.     }
  45.     regular = fontinfo->fid;
  46.     W_Textwidth = fontinfo->max_bounds.width;
  47.     W_Textheight = fontinfo->max_bounds.descent + fontinfo->max_bounds.ascent;
  48.     fonts[1].baseline = fontinfo->max_bounds.ascent;
  49.     fonts[1].fontstruct = fontinfo;
  50.  
  51.     if (DisplayCells(W_Display, W_Screen) <= 4)
  52.     fontname = BOLD_FONT2;
  53.     else
  54.     fontname = BOLD_FONT;
  55.     fontinfo = XLoadQueryFont(W_Display, fontname);
  56.     if (fontinfo == NULL) {
  57.     fontinfo = find_font(fontname, _bfonts);
  58.     }
  59.     if (fontinfo == NULL) {
  60.     bold = regular;
  61.     fonts[2].baseline = fonts[1].baseline;
  62.     fonts[2].fontstruct = fonts[1].fontstruct;
  63.     } else {
  64.     bold = fontinfo->fid;
  65.     fonts[2].baseline = fontinfo->max_bounds.ascent;
  66.     fonts[2].fontstruct = fontinfo;
  67.     if (fontinfo->max_bounds.width > W_Textwidth)
  68.         W_Textwidth = fontinfo->max_bounds.width;
  69.     if (fontinfo->max_bounds.descent + fontinfo->max_bounds.ascent > W_Textheight)
  70.         W_Textheight = fontinfo->max_bounds.descent + fontinfo->max_bounds.ascent;
  71.     }
  72.  
  73.     if (DisplayCells(W_Display, W_Screen) <= 4)
  74.     fontname = ITALIC_FONT2;
  75.     else
  76.     fontname = ITALIC_FONT;
  77.     fontinfo = XLoadQueryFont(W_Display, fontname);
  78.     if (fontinfo == NULL) {
  79.     fontinfo = find_font(fontname, _ifonts);
  80.     }
  81.     if (fontinfo == NULL) {
  82.     italic = regular;
  83.     fonts[3].baseline = fonts[1].baseline;
  84.     fonts[3].fontstruct = fonts[1].fontstruct;
  85.     } else {
  86.     italic = fontinfo->fid;
  87.     fonts[3].baseline = fontinfo->max_bounds.ascent;
  88.     fonts[3].fontstruct = fontinfo;
  89.     if (fontinfo->max_bounds.width > W_Textwidth)
  90.         W_Textwidth = fontinfo->max_bounds.width;
  91.     if (fontinfo->max_bounds.descent + fontinfo->max_bounds.ascent > W_Textheight)
  92.         W_Textheight = fontinfo->max_bounds.descent + fontinfo->max_bounds.ascent;
  93.     }
  94.  
  95.     fontname = BIG_FONT;
  96.     fontinfo = XLoadQueryFont(W_Display, fontname);
  97.     if (fontinfo == NULL) {
  98.     fontinfo = find_font(fontname, _bgfonts);
  99.     }
  100.     if (fontinfo == NULL) {
  101.     big = regular;
  102.     fonts[0].baseline = fonts[1].baseline;
  103.     fonts[0].fontstruct = fonts[1].fontstruct;
  104.     } else {
  105.     big = fontinfo->fid;
  106.     fonts[0].baseline = fontinfo->max_bounds.ascent;
  107.     fonts[0].fontstruct = fontinfo;
  108.     W_BigTextwidth = fontinfo->max_bounds.width;
  109.     W_BigTextheight = fontinfo->max_bounds.descent + fontinfo->max_bounds.ascent;
  110.     }
  111.     for (i = 0; i < NCOLORS; i++) {
  112.     values.font = big;
  113.     colortable[i].contexts[0] = XCreateGC(W_Display, W_Root, GCFont, &values);
  114.     XSetGraphicsExposures(W_Display, colortable[i].contexts[0], False);
  115.     values.font = regular;
  116.     colortable[i].contexts[1] = XCreateGC(W_Display, W_Root, GCFont, &values);
  117.     XSetGraphicsExposures(W_Display, colortable[i].contexts[1], False);
  118.     values.font = bold;
  119.     colortable[i].contexts[2] = XCreateGC(W_Display, W_Root, GCFont, &values);
  120.     XSetGraphicsExposures(W_Display, colortable[i].contexts[2], False);
  121.     values.font = italic;
  122.     colortable[i].contexts[3] = XCreateGC(W_Display, W_Root, GCFont, &values);
  123.     XSetGraphicsExposures(W_Display, colortable[i].contexts[3], False);
  124.     {
  125.         static char dl[] = {1, 4};
  126.         XSetLineAttributes(W_Display, colortable[i].contexts[3],
  127.                    0, LineOnOffDash, CapButt, JoinMiter);
  128.         XSetDashes(W_Display, colortable[i].contexts[3], 0, dl, 2);
  129.     }
  130.     values.function = GXor;
  131.     colortable[i].contexts[BITGC] = XCreateGC(W_Display, W_Root, GCFunction, &values);
  132.     XSetGraphicsExposures(W_Display, colortable[i].contexts[BITGC], False);
  133.     colortable[i].contexts[BITMASKGC] = XCreateGC(W_Display, W_Root, 0, &values);
  134.     XSetGraphicsExposures(W_Display, colortable[i].contexts[BITMASKGC], 
  135.                   False);
  136.     }
  137.     values.fill_style = FillSolid;
  138.     values.line_style = LineSolid;
  139.     borderGC = XCreateGC(W_Display, W_Root, GCFillStyle|GCLineStyle, &values);
  140.  
  141.     values.function = GXand;
  142.     maskGC = XCreateGC(W_Display, W_Root,
  143.         GCFunction , &values);
  144. }
  145.  
  146. static void
  147. GetColors()
  148. {
  149.     int     i, j;
  150.     XColor  foo;
  151.     int     white, black;
  152. /*    unsigned long pixel;
  153.     unsigned long planes[3];*/
  154.     char    defaultstring[100];
  155.     char   *defs;
  156. #ifdef FOURPLANEFIX
  157.     unsigned long extracolors[8];
  158.     XColor  colordef;
  159. #endif
  160.     extern int forceMono;
  161.     forceMono = 0;
  162.  
  163.     if ((DisplayCells(W_Display, W_Screen) <= 4) || forceMono) {
  164.     forceMono = 1;
  165.     white = WhitePixel(W_Display, W_Screen);
  166.     black = BlackPixel(W_Display, W_Screen);
  167.     for (i = 0; i < NCOLORS; i++) {
  168.         if (i != W_Black) {
  169.         colortable[i].pixelValue = white;
  170.         } else {
  171.         colortable[i].pixelValue = black;
  172.         }
  173.         if (i == W_Red) {
  174.         colortable[i].pixmap = XCreatePixmapFromBitmapData
  175.             (W_Display,
  176.              W_Root, (char *) striped, TILESIDE, TILESIDE,
  177.              (unsigned)white, (unsigned)black,
  178.              (unsigned)DefaultDepth(W_Display, W_Screen));
  179.         } else if (i == W_Yellow) {
  180.         colortable[i].pixmap = XCreatePixmapFromBitmapData
  181.             (W_Display,
  182.              W_Root, (char *) gray, TILESIDE, TILESIDE,
  183.              (unsigned)white, (unsigned)black,
  184.              (unsigned)DefaultDepth(W_Display, W_Screen));
  185.         } else {
  186.         colortable[i].pixmap = XCreatePixmapFromBitmapData
  187.             (W_Display,
  188.              W_Root, (char *) solid, TILESIDE, TILESIDE,
  189.              (unsigned)colortable[i].pixelValue,
  190.              (unsigned)colortable[i].pixelValue,
  191.              (unsigned)DefaultDepth(W_Display, W_Screen));
  192.         }
  193.  
  194.         /*
  195.            We assume white is 0 or 1, and black is 0 or 1. We adjust
  196.            graphics function based upon who is who.
  197.         */
  198.         if (white == 0) {    /* Black is 1 */
  199.         XSetFunction(W_Display, colortable[i].contexts[BITGC], GXand);
  200.         }
  201.     }
  202.     } else if (DefaultVisual(W_Display, W_Screen)->class == TrueColor) {
  203. /* Stuff added by sheldon@iastate.edu 5/28/93
  204.  * This is supposed to detect a TrueColor display, and then do a lookup of
  205.  * the colors in default colormap, instead of creating new colormap
  206.  */
  207.     for (i = 0; i < NCOLORS; i++) {
  208.         sprintf(defaultstring, "color.%s", colortable[i].name);
  209.  
  210. /*        defs = stringDefault(defaultstring,NULL);
  211.         if (defs == NULL)
  212. */
  213.             defs = colortable[i].name;
  214.         XParseColor(W_Display, W_Colormap, defs, &foo);
  215.         XAllocColor(W_Display, W_Colormap, &foo);
  216.         colortable[i].pixelValue = foo.pixel;
  217.         colortable[i].pixmap = XCreatePixmapFromBitmapData(W_Display,
  218.         W_Root, (char *) solid, TILESIDE, TILESIDE, (unsigned)foo.pixel, 
  219.         (unsigned)foo.pixel, (unsigned)DefaultDepth(W_Display, W_Screen));
  220.     }
  221.     } else {
  222. #ifdef FOURPLANEFIX
  223. #ifndef HACKED_XPMLIB
  224.     xpmORplanes = DefaultDepth(W_Display,W_Screen);
  225. #endif /*HACKED_XPMLIB*/
  226.         nplanes = (xpm && useOR) ? xpmORplanes : 8;
  227.     nplanes = 7;
  228.     if (!XAllocColorCells(W_Display, W_Colormap, False, planes, nplanes,
  229.                   &base, 1)) {
  230.         /* couldn't allocate (nplanes) planes, make a new colormap */
  231.         printf("Couldn't allocate %d planes for private use, so allocating a private colormap.\n",nplanes);
  232.         W_Colormap = XCreateColormap(W_Display, W_Root, W_Visual, AllocNone);
  233.         if (!XAllocColorCells(W_Display, W_Colormap, False, planes, 3/*nplanes*/,
  234.                   &base, 1)) {
  235.         fprintf(stderr, "Cannot create new colormap\n");
  236.         exit(1);
  237.         }
  238.         /*
  239.            and fill it with at least 8 more colors so when mouse is
  240.            inside xGalaga windows, use might be able to see his other
  241.            windows
  242.         */
  243.         if (XAllocColorCells(W_Display, W_Colormap, False, NULL, 0,
  244.                  extracolors, 8)) {
  245.         colordef.flags = DoRed | DoGreen | DoBlue;
  246.         for (i = 0; i < 8; i++) {
  247.             colordef.pixel = extracolors[i];
  248.             colordef.red = extrared[i] << 8;
  249.             colordef.green = extragreen[i] << 8;
  250.             colordef.blue = extrablue[i] << 8;
  251.             XStoreColor(W_Display, W_Colormap, &colordef);
  252.         }
  253.         }
  254.     }
  255. #else
  256.     XAllocColorCells(W_Display, W_Colormap, False, planes, nplanes, &base, 1);
  257. #endif
  258.     nallocated_colors = NCOLORS + 1;
  259.     for (i = 0; i < NCOLORS; i++) {
  260.         /*
  261.            strcpy(defaultstring, "color.%s", colortable[i].name);
  262.         */
  263.         sprintf(defaultstring, "color.%s", colortable[i].name);
  264.  
  265.         defs = strdup(colortable[i].name);
  266.         XParseColor(W_Display, W_Colormap, defs, &foo);
  267.         /*
  268.            Black must be the color with all the planes off. That is the
  269.            only restriction I concerned myself with in the following case
  270.            statement.
  271.         */
  272.         switch (i) {
  273.         case WHITE:
  274.         foo.pixel = base | planes[0] | planes[1] | planes[2];
  275.         memcpy(&allocated_colors[7], &foo, sizeof(foo));
  276.         break;
  277.         case BLACK:
  278.         foo.pixel = base;
  279.         memcpy(&allocated_colors[0], &foo, sizeof(foo));
  280.         break;
  281.         case RED:
  282.         foo.pixel = base | planes[0];
  283.         memcpy(&allocated_colors[1], &foo, sizeof(foo));
  284.         break;
  285.         case CYAN:
  286.         foo.pixel = base |             planes[1];
  287.         memcpy(&allocated_colors[2], &foo, sizeof(foo));
  288.         break;
  289.         case YELLOW:
  290.         foo.pixel = base |                         planes[2];
  291.         memcpy(&allocated_colors[4], &foo, sizeof(foo));
  292.         break;
  293.         case DARK_GREY:
  294.         foo.pixel = base | planes[0] | planes[1];
  295.         memcpy(&allocated_colors[3], &foo, sizeof(foo));
  296.         break;
  297.         case GREEN:
  298.         foo.pixel = base |             planes[1] | planes[2];
  299.         memcpy(&allocated_colors[6], &foo, sizeof(foo));
  300.         break;
  301.         case LIGHT_GREY:
  302.             foo.pixel = base | planes[0] |             planes[2];
  303.         memcpy(&allocated_colors[5], &foo, sizeof(foo));
  304.                 break;
  305.         case GREEN2:
  306.         foo.pixel = base | planes[3];
  307.         memcpy(&allocated_colors[8], &foo, sizeof(foo));
  308.         break;
  309.           case BLUE:
  310.         foo.pixel = base | planes[3] | planes[0];
  311.         memcpy(&allocated_colors[9], &foo, sizeof(foo));
  312.         break;
  313.           case DARKGREEN:
  314.         foo.pixel = base | planes[3] | planes[1];
  315.         memcpy(&allocated_colors[10], &foo, sizeof(foo));
  316.         break;
  317.           case BROWN:
  318.         foo.pixel = base | planes[3] | planes[1] | planes[0];
  319.         memcpy(&allocated_colors[11], &foo, sizeof(foo));
  320.         break;
  321.         }
  322.         XStoreColor(W_Display, W_Colormap, &foo);
  323.         colortable[i].pixelValue = foo.pixel;
  324.         colortable[i].pixmap = XCreatePixmapFromBitmapData(W_Display,
  325.         W_Root, (char *) solid, TILESIDE, TILESIDE, foo.pixel, foo.pixel,
  326.         (unsigned) DefaultDepth(W_Display, W_Screen));
  327.     }
  328.     }
  329.     for (i = 0; i < NCOLORS; i++) {
  330.     for (j = 0; j < FONTS + NONFONTS; j++) {
  331.         XSetForeground(W_Display, colortable[i].contexts[j],
  332.                colortable[i].pixelValue);
  333.         XSetBackground(W_Display, colortable[i].contexts[j],
  334.                colortable[W_Black].pixelValue);
  335.     }
  336.     }
  337.     /* set FG/BG for border GC [BDyess] */
  338.     XSetBackground(W_Display, borderGC, colortable[DARK_GREY].pixelValue);
  339.     XSetForeground(W_Display, borderGC, colortable[LIGHT_GREY].pixelValue);
  340.  
  341.     XSetBackground(W_Display, maskGC, 0xff);
  342.     XSetForeground(W_Display, maskGC, 0);
  343.  
  344.     backColor = W_Black;
  345. }
  346.  
  347. void
  348. W_Initialize(str)
  349.     char   *str;
  350. {
  351.     int     i;
  352.  
  353. #ifdef DEBUG
  354.     printf("Initializing...\n");
  355. #endif
  356.     for (i = 0; i < HASHSIZE; i++) {
  357.     hashtable[i] = NULL;
  358.     }
  359.     if ((W_Display = XOpenDisplay(str)) == NULL) {
  360.     fprintf(stderr, "Cannot open display \"%s\"\n", str ? str : "(null)");
  361.     exit(1);
  362.     }
  363. #ifdef ABORT_ON_ERROR
  364.     /* tmp */
  365.     XSynchronize(W_Display, True);
  366.     XSetErrorHandler(_myerror);
  367. #endif
  368.  
  369.     W_Root = DefaultRootWindow(W_Display);
  370. #ifdef FOURPLANEFIX
  371.     W_Visual = DefaultVisual(W_Display, DefaultScreen(W_Display));
  372. #endif
  373.     W_Screen = DefaultScreen(W_Display);
  374.     W_Colormap = DefaultColormap(W_Display, W_Screen);
  375.     myroot.window = W_Root;
  376.     myroot.type = WIN_GRAPH;
  377. /*
  378.     imagedir = strdup(IMAGEDIR);
  379.     if(verbose_image_loading) printf("imagedir = %s\n",imagedir);
  380. */
  381.     GetFonts();
  382.     GetColors();
  383. }
  384.  
  385.